﻿using System.ComponentModel.Composition;
using System.Linq;
using System.Xml.Linq;

namespace Kindler.Processors
{
    [Export(typeof(IProcessor))]
    public class LinkHrefDisplayerProcessor : IProcessor
    {
        public void Process(XDocument htmlBook)
        {
            var links = from a in htmlBook.Descendants("a")
                        let href = a.Attribute("href")
                        where href != null && !string.IsNullOrEmpty(href.Value) && href.Value.StartsWith("http")
                        select a;

            foreach (var link in links)
            {
                // Add the href value beside the existing link so that it can be
                // easily read
                link.AddAfterSelf(new XElement("span",
                    new XElement("i", string.Format(" ({0})", link.Attribute("href").Value))));
            }
        }
    }
}